package com.frejoys.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.frejoys.common.vo.app.VerifyActivityListVo;
import com.frejoys.dao.entity.StoreActivity;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;


public interface StoreActivityMapper extends BaseMapper<StoreActivity> {

    // 获取店铺活动id
    @Select("select id from j_store_activity where store_id=#{storeId} and start_time<#{nowTime} and end_time>#{nowTime} and shelves=#{shelves} and status=#{status} limit 1")
    Integer getValidActivityId(@Param("storeId") Integer storeId, @Param("nowTime") Integer nowTime, @Param("shelves") Integer shelves, @Param("status") Integer status);


    // 核销订单，获取活动信息
    @Select("select start_ratio,end_ratio,allow_count,rebate_ratio,promotion_ratio,type from j_store_activity where id=#{id} limit 1")
    StoreActivity getVerifyInfo(@Param("id") Integer id);
    // @Select("select rebate_ratio,promotion_ratio,commission_ratio,type from j_store_activity where id=#{id} and start_time<#{nowTime} and end_time>#{nowTime} and shelves=#{shelves} and status=#{status} limit 1")
    // StoreActivity getVerifyInfo(@Param("id") Integer id, @Param("nowTime") Integer nowTime, @Param("shelves") Integer shelves, @Param("status") Integer status);

    // 更新允许的总返利人数
    @Update("update j_store_activity set allow_count=allow_count+#{allowCount} where id=#{id}")
    boolean upAllowCount(@Param("id") Integer id, @Param("allowCount") Integer allowCount);

    // 返利金额放入店铺活动金额池，增加人数
    @Update("update j_store_activity set money=money+#{money},total_money=total_money+#{money},person_count=person_count+1,total_person_count=total_person_count+1 where id=#{id}")
    boolean upActivityInfo(@Param("id") Integer id, @Param("money") BigDecimal money);

    // 更新活动当前金额 增加返利人数，减少当前当前参与人数
    @Update("update j_store_activity set money=money+#{money},person_count=person_count-1,rebate_count=rebate_count+1 where id=#{id}")
    boolean upActivityMoney(@Param("id") Integer id, @Param("money") BigDecimal money);

    // 获取上架状态
    @Select("select shelves,store_id,start_time,end_time from j_store_activity where id=#{id} limit 1")
    StoreActivity getShelvesInfo(@Param("id") Integer commonId);

    // 更新上架状态
    @Update("update j_store_activity set shelves=#{shelves} where id=#{id}")
    Boolean upShelvesInfo(@Param("id") Integer id, @Param("shelves") Integer shelves);

    List<StoreActivity> totalMoneyAll(@Param("storeIds") List<Integer> storeIds, @Param("time") Integer time,@Param("shelves") Integer shelves,@Param("status") Integer status);

    //查询活动信息
    List<VerifyActivityListVo> verifyActivityList(@Param("storeIds") List<Integer> storeIds, @Param("status") Integer status);

    /**
     * 根据店铺活动ids查询店铺活动类型
     */
    List<StoreActivity> getTypeByIds(@Param("activityIds") List<Integer> activityIds);

    /**
     * 查询该店铺活动上架时刻
     */
    @Select("select count(id) from j_store_activity where store_id = #{storeId} and shelves > 0 and ((#{startTime} BETWEEN start_time AND end_time) OR\n" +
            "    (#{endTime} BETWEEN start_time AND end_time) OR\n" +
            "    (start_time BETWEEN #{startTime} AND #{endTime}) OR\n" +
            "    (end_time BETWEEN #{startTime} AND #{endTime}))")
    Integer getStoreActivity(@Param("storeId") Integer storeId, @Param("startTime") Integer startTime, @Param("endTime") Integer endTime);

    /**
     * 查询该店铺活动上架数量 不包括自身
     */
    @Select("select count(id) from j_store_activity where store_id = #{storeId} and shelves > 0 and id != #{id} and ((#{startTime} BETWEEN start_time AND end_time) OR\n" +
            "    (#{endTime} BETWEEN start_time AND end_time) OR\n" +
            "    (start_time BETWEEN #{startTime} AND #{endTime}) OR\n" +
            "    (end_time BETWEEN #{startTime} AND #{endTime}))")
    Integer getStoreActivityNotOwn(@Param("id") Integer id,@Param("storeId") Integer storeId, @Param("startTime") Integer startTime, @Param("endTime") Integer endTime);

}
